<?php
declare(strict_types=1);

namespace longbingcore\wxcore;

use app\Common\LongbingServiceNotice;

use think\facade\Db;

/**
 * 达达开放平台sdk
 * User: 仝帅
 * Date: 2016-12-19
 * Time: 16:48
 */
class PospalApi
{

    protected $appId;

    protected $appKey;

    protected $signature;

    protected $url;

    public function __construct()
    {
//        $this->appId  = '205871490509191130';
        $this->appId  = '9C7F3BFC37E729D9A5D6798B2D358E28';

        $this->url    = 'https://area56-win.pospal.cn:443/';

        $this->account_key = '0e73963b251112af12835cd7d6dbadca';
    }


    /**
     * @author chenniang
     * @DataTime: 2022-02-17 17:42
     * @功能说明:获取令牌
     */

    public function getToken(){

        $url = 'https://api.ubibot.cn/accounts/generate_access_token?account_key='.$this->account_key;

        $key = 'ubibot_token';

        $data = getCache($key);

        if(empty($data)){

            $data = $this->request($url,[],'GET');

            if(!empty($data['result'])&&$data['result']=='success'){

                setCache($key,$data,60);
            }

        }

        return $data;
    }


    /**
     * @author chenniang
     * @DataTime: 2022-02-17 17:54
     * @功能说明:获取空间列表
     */
    public function getRoomList(){

        $url = 'https://api.ubibot.cn/channels?account_key='.$this->account_key;

        $data = $this->request($url,[],'GET');

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2022-02-17 17:57
     * @功能说明:获取空间详情
     */
    public function getRoomInfo($room_id){

        $url = 'https://api.ubibot.cn/channels/'.$room_id.'?account_key='.$this->account_key;

        $data = $this->request($url,[],'GET');

        return $data;


    }





    /**
     * @param $url
     * @param $post_data
     * @param string $post_type
     * @功能说明:发送请求
     * @author chenniang
     * @DataTime: 2021-10-26 14:38
     */
    public function request($url,$post_data,$post_type='POST'){

        $curl = curl_init();

        $post_data = json_encode($post_data);

        curl_setopt_array($curl, array(
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => $post_type,
            CURLOPT_POSTFIELDS => $post_data,
            CURLOPT_HTTPHEADER => array(
                "Content-Type:application/json",
                "User-Agent:openApi",
                "Content-Type:application/json; charset=utf-8",
                "accept-encoding:gzip,deflate",
                "time-stamp:".time(),
//                "data-signature:D2C3D3C7289EAE111277999D21196D4D",
                "data-signature:".strtoupper(md5($this->appKey.$post_data))
            ),
        ));

        $response = curl_exec($curl);

        $err = curl_error($curl);

        curl_close($curl);

        return json_decode($response,true);

//        if ($err) {
//            echo "cURL Error #:" . $err;
//        } else {
//            echo $response;
//        }
    }





}